package org.istt.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.mail.AuthenticationFailedException;
import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.components.ActionError;
import org.istt.domain.FileBoSungCCHN;
import org.istt.domain.FileCapCCHN;
import org.istt.domain.FileCapCCMGBDS;
import org.istt.domain.FileCapLaiCCHN;
import org.istt.domain.FileCapPhepXayDung;
import org.istt.domain.FileDieuChinhGPXD;
import org.istt.domain.FileGiaHanCPXD;
import org.istt.export.BmQT_Notify;
import org.istt.model.NotificationBean;
import org.istt.service.FileBoSungCCHNManager;
import org.istt.service.FileCapCCHNManager;
import org.istt.service.FileCapCCMGBDSManager;
import org.istt.service.FileCapGPXDManager;
import org.istt.service.FileCapLaiCCHNManager;
import org.istt.service.FileDieuChinhGPXDManager;
import org.istt.service.FileGiaHanGPXDManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 *
 */
public class FilePublicAdministrationAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 9012754123343759656L;
	private String address;
	private Boolean chkReceived=false;
	private String delete;
	private String email;
	private String file_sender;
	private FileBoSungCCHN fileBoSungCCHN;
	private FileBoSungCCHNManager fileBoSungCCHNMgr;
	
	private FileCapCCHN fileCapCCHN;
	private FileCapCCHNManager fileCapCCHNMgr;
	private FileCapCCMGBDS fileCapCCMGBDS;
	private FileCapCCMGBDSManager fileCapCCMGBDSMgr;
	private FileCapLaiCCHN fileCapLaiCCHN;
	private FileCapLaiCCHNManager fileCapLaiCCHNMgr;
	private FileCapPhepXayDung fileCapPhepXayDung;
	private FileCapGPXDManager fileCapPhepXDMgr; 
	
	private String fileCode;
	private FileDieuChinhGPXD fileDieuChinhGPXD;
	private FileDieuChinhGPXDManager fileDieuChinhGPXDMgr;
	private FileGiaHanCPXD fileGiaHanCPXD;
	private FileGiaHanGPXDManager fileGiaHanCPXDMgr;

	private List<List<Object>> filePubAdmins;
	private String fromDate;



	private Long id;
	
	private String informMessage;

	private String informNote;

	private int informType;
	
	private final Log log = LogFactory.getLog(FilePublicAdministrationAction.class);

	private String phone;
	
	private String search;

	private String sender;

    private String subject;
    
    private String toDate;	
	
	private Integer type;
	
	public String delete() {
        if (log.isDebugEnabled()) {
            log.debug("entering 'delete' method");
        }	
        
		if (id != null) {
			switch(type)
			{
			  case 1:
				  fileCapPhepXDMgr.remove(id);
				  setDeleteMessage();
				  break;			
			  case 2:
				  fileCapCCHNMgr.remove(id);
				  setDeleteMessage();
				  break;
			  case 3:
				  fileCapLaiCCHNMgr.remove(id);
				  setDeleteMessage();
				  break;
			  case 4:
				  fileDieuChinhGPXDMgr.remove(id);
				  setDeleteMessage();
				  break;
			  case 5:
				  fileGiaHanCPXDMgr.remove(id);
				  setDeleteMessage();
				  break;
			  case 6:
				  fileBoSungCCHNMgr.remove(id);
				  setDeleteMessage();
				  break;
			  case 7:
				  fileCapCCMGBDSMgr.remove(id);
				  setDeleteMessage();
				  break;
			}
		}        
		else {
			log.debug("entity is null");
		}
        return "delete";
    }
	
	public String execute() {
        if (delete != null) {
            return delete();
        }
        
        if (search != null) {
            return "search";
        }
        
        return SUCCESS;
    }

	public String fileDetail() {
		if (log.isDebugEnabled()) {
			log.debug("entering 'fileDetail' method");
		}

		if (id != null) {
			switch(type)
			{
			  case 1:
				  this.fileCapPhepXayDung = fileCapPhepXDMgr.get(id);
				  break;			
			  case 2:
				  this.fileCapCCHN = fileCapCCHNMgr.get(id);
				  break;
			  case 3:
				  this.fileCapLaiCCHN = fileCapLaiCCHNMgr.get(id);
				  break;
			  case 4:
				  this.fileDieuChinhGPXD = fileDieuChinhGPXDMgr.get(id);
				  break;
			  case 5:
				  this.fileGiaHanCPXD = fileGiaHanCPXDMgr.get(id);
				  break;
			  case 6:
				  this.fileBoSungCCHN = fileBoSungCCHNMgr.get(id);
				  break;
			  case 7:
				  this.fileCapCCMGBDS = fileCapCCMGBDSMgr.get(id);
				  break;
			}
		}
		return SUCCESS;
	}

	public String getAddress() {
		return address;
	}

	public Boolean getChkReceived() {
		return chkReceived;
	}

	public String getDelete() {
		return delete;
	}


	public String getEmail() {
		return email;
	}


	public String getFile_sender() {
		return file_sender;
	}
	
	
	public FileBoSungCCHN getFileBoSungCCHN() {
		return fileBoSungCCHN;
	}	
	public FileBoSungCCHNManager getFileBoSungCCHNMgr() {
		return fileBoSungCCHNMgr;
	}
	public FileCapCCHN getFileCapCCHN() {
		return fileCapCCHN;
	}
	public FileCapCCMGBDS getFileCapCCMGBDS() {
		return fileCapCCMGBDS;
	}
	public FileCapCCMGBDSManager getFileCapCCMGBDSMgr() {
		return fileCapCCMGBDSMgr;
	}
	public FileCapLaiCCHN getFileCapLaiCCHN() {
		return fileCapLaiCCHN;
	}
	public FileCapPhepXayDung getFileCapPhepXayDung() {	
		return fileCapPhepXayDung;
	}

	public String getFileCode() {
		return fileCode;
	}

	public FileDieuChinhGPXD getFileDieuChinhGPXD() {
		return fileDieuChinhGPXD;
	}

	public FileGiaHanCPXD getFileGiaHanCPXD() {
		return fileGiaHanCPXD;
	}

	public List<List<Object>> getFilePubAdmins() {
		return filePubAdmins;
	}

	public String getFromDate() {
		return fromDate;
	}

	public Long getId() {
		//return (FileCapPhepXayDung==null? id : FileCapPhepXayDung.getId());//id
		return id;
	}

	public String getInformMessage() {
		return informMessage;
	}

	public String getInformNote() {
		return informNote;
	}

	public int getInformType() {
		return informType;
	}

	public String getPhone() {
		return phone;
	}
	public String getSearch() {
		return search;
	}
	public String getSender() {
		return sender;
	}
	public String getSubject() {
		return subject;
	}
	public String getToDate() {
		return toDate;
	}
	
	
	public Integer getType() {
		return type;
	}
	
	public String receive() {
        if (log.isDebugEnabled()) {
            log.debug("entering 'receive' method");
        }	
        return SUCCESS;
    }
	
	public String search() throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("entering 'search' method");
		}
		Date f = StringUtils.isEmpty(fromDate) ? null : DateUtils.parseDate(
				fromDate, new String[] { getText("date.format") });
		Date t = StringUtils.isEmpty(toDate) ? null : DateUtils.parseDate(
				toDate, new String[] { getText("date.format") });		
		
		filePubAdmins = fileCapPhepXDMgr.searchAllObjects(f, t, chkReceived);
		
		return SUCCESS;
	}
	
	public String sendMessage() {
		NotificationBean bean = new NotificationBean();
		bean.setAddress(address);
		bean.setFileCode(fileCode);
		bean.setFullname(sender);
		bean.setEmail(this.email);
		
		if(StringUtils.isNotEmpty(this.email) && StringUtils.isNotEmpty(this.fileCode)){
			
			String notice = getText(" \u0110\u00E3 \u0111\u1EE7 h\u1ED3 s\u01A1!");
			if(informType == 1) notice = getText(" \u0110\u00E3 \u0111\u1EE7 h\u1ED3 s\u01A1!");
			if(informType == 2) notice = getText(" Ch\u01B0a \u0111\u1EE7 h\u1ED3 s\u01A1!");
			
			informMessage = getText("S\u1EDF X\u00E2y d\u1EF1ng t\u1EC9nh B\u00ECnh D\u01B0\u01A1ng xin th\u00F4ng b\u00E1o: h\u1ED3 s\u01A1 m\u00E3 s\u1ED1 ") 
							+ fileCode + " " + notice + " " + this.informNote;
			bean.setContent(informMessage);
			
			//Step 1: Save file Thong bao
			ServletContext servletContext = ServletActionContext.getServletContext();
		    String fileName = "D://"+this.fileCode+"_Thong_Bao.doc";
			try {
				
				new BmQT_Notify().saveToMSWord(new FileInputStream(new File(servletContext.getRealPath("/word/MauThongBao.docx"))), fileName, bean);
				
				log.debug("SAVE FILE SUCCESSFUL!");
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//Step 2: Send email with attachment file
			ApplicationContext context = new ClassPathXmlApplicationContext("spring-mail.xml");
			
			MailService mm = (MailService) context.getBean("mailService");
			
			try {
				
				// From - To - Subject - Content - Attachfile
				mm.sendMail(mm.getSender(), this.email, subject, informMessage, fileName);
				
				log.debug("SEND MAIL SUCCESSFUL!");
				
				ActionContext.getContext().getSession().put("message", getText("online.send.email.succesful"));
				
			} catch (AuthenticationFailedException e) {
				e.printStackTrace();
				ActionContext.getContext().getSession().put("message", getText("online.send.email.failure.authenticate"));
			} catch (Exception e) {
				e.printStackTrace();
				ActionContext.getContext().getSession().put("message", getText("online.send.email.failure"));
			}
			
			//Step 3: Delete file
			File deleteFile = new File(fileName);
			deleteFile.delete();

			//Step 4: Save Note to DB
			updateNotify(informMessage);
			
		}
		switch(type)
		{
		  case 1:
			  return "success_type_1";
			  
		  case 2:
			  return "success_type_2";
			  
		  case 3:
			  return "success_type_3";
			  
		  case 4:
			  return "success_type_4";
			  
		  case 5:
			  return "success_type_5";
			  
		  case 6:
			  return "success_type_6";
			  
		  case 7:
			  return "success_type_7";
			  
		}
		return SUCCESS;
	}	
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public void setChkReceived(Boolean chkReceived) {
		this.chkReceived = chkReceived;
	}	
	
	public void setDelete(String delete) {
        this.delete = delete;
    }	
	
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("file.deleted"));
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public void setFile_sender(String file_sender) {
		this.file_sender = file_sender;
	}
	
	public void setFileBoSungCCHN(FileBoSungCCHN fileBoSungCCHN) {
		this.fileBoSungCCHN = fileBoSungCCHN;
	}

	@Autowired
	public void setFileBoSungCCHNMgr(FileBoSungCCHNManager fileBoSungCCHNMgr) {
		this.fileBoSungCCHNMgr = fileBoSungCCHNMgr;
	}

	@Autowired
	public void setFileCapCCHNMgr(FileCapCCHNManager fileCapCCHNMgr) {
		this.fileCapCCHNMgr = fileCapCCHNMgr;
	}

	public void setFileCapCCMGBDS(FileCapCCMGBDS fileCapCCMGBDS) {
		this.fileCapCCMGBDS = fileCapCCMGBDS;
	}

	@Autowired
	public void setFileCapCCMGBDSMgr(FileCapCCMGBDSManager fileCapCCMGBDSMgr) {
		this.fileCapCCMGBDSMgr = fileCapCCMGBDSMgr;
	}
	
	@Autowired
	public void setFileCapLaiCCHNMgr(FileCapLaiCCHNManager fileCapLaiCCHNMgr) {
		this.fileCapLaiCCHNMgr = fileCapLaiCCHNMgr;
	}

	@Autowired
	public void setFileCapPhepXDMgr(FileCapGPXDManager fileCapPhepXDMgr) {
		this.fileCapPhepXDMgr = fileCapPhepXDMgr;
	}

	public void setFileCode(String fileCode) {
		this.fileCode = fileCode;
	}

	@Autowired
	public void setFileDieuChinhGPXDMgr(
			FileDieuChinhGPXDManager fileDieuChinhGPXDMgr) {
		this.fileDieuChinhGPXDMgr = fileDieuChinhGPXDMgr;
	}

	@Autowired
	public void setFileGiaHanCPXDMgr(FileGiaHanGPXDManager fileGiaHanCPXDMgr) {
		this.fileGiaHanCPXDMgr = fileGiaHanCPXDMgr;
	}
	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public void setId(Long id) {
		this.id = id;
	}
	public void setInformMessage(String informMessage) {
		this.informMessage = informMessage;
	}

	public void setInformNote(String informNote) {
		this.informNote = informNote;
	}

	public void setInformType(int informType) {
		this.informType = informType;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	protected void setSaveMessage() {
		ActionContext.getContext().getSession()
		.put("message", getText("file.received"));		
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public String showInform(){
		// Get File Detail
		if (id != null) {
			switch(type)
			{
			  case 1:
				  this.fileCapPhepXayDung = fileCapPhepXDMgr.get(id);
				  if(this.fileCapPhepXayDung!=null){
					  this.fileCode = this.fileCapPhepXayDung.getFileCode();
					  this.sender = this.fileCapPhepXayDung.getTen_lien_he();
					  this.email = this.fileCapPhepXayDung.getEmail();
					  this.phone = this.fileCapPhepXayDung.getDien_thoai();
					  this.address = this.fileCapPhepXayDung.getDia_chi_lien_he();
				  }
				  break;			
				  
			  case 2:
				  this.fileCapCCHN = fileCapCCHNMgr.get(id);
				  if(this.fileCapCCHN!=null){
					  fileCode = this.fileCapCCHN.getFileCode();
					  this.sender = this.fileCapCCHN.getTen_lien_he();
					  this.email = this.fileCapCCHN.getEmail();
					  this.address = this.fileCapCCHN.getDiachi_thuongtru();
				  }
				  break;
				  
			  case 3:
				  this.fileCapLaiCCHN = fileCapLaiCCHNMgr.get(id);
				  if(this.fileCapLaiCCHN!=null){
					  fileCode = this.fileCapLaiCCHN.getFileCode();
					  this.sender = this.fileCapLaiCCHN.getContactName();
					  this.email = this.fileCapLaiCCHN.getEmail();
					  this.address = this.fileCapLaiCCHN.getAddress();
				  }
				  break;
				  
			  case 4:
				  this.fileDieuChinhGPXD = fileDieuChinhGPXDMgr.get(id);
				  if(this.fileDieuChinhGPXD!=null){
					  fileCode = this.fileDieuChinhGPXD.getFileCode();
					  this.sender = this.fileDieuChinhGPXD.getTen_lien_he();
					  this.email = this.fileDieuChinhGPXD.getEmail();
					  this.phone = this.fileDieuChinhGPXD.getDien_thoai();
				  }
				  break;
				  
			  case 5:
				  this.fileGiaHanCPXD = fileGiaHanCPXDMgr.get(id);
				  if(this.fileGiaHanCPXD!=null){
					  fileCode = this.fileGiaHanCPXD.getFileCode();
					  this.sender = this.fileGiaHanCPXD.getTen_lien_he();
					  this.email = this.fileGiaHanCPXD.getEmail();
					  this.phone = this.fileGiaHanCPXD.getDien_thoai();
				  }
				  break;
				  
			  case 6:
				  this.fileBoSungCCHN = fileBoSungCCHNMgr.get(id);
				  if(this.fileBoSungCCHN!=null){
					  fileCode = this.fileBoSungCCHN.getFileCode();
					  this.sender = this.fileBoSungCCHN.getTen_lien_he();
					  this.email = this.fileBoSungCCHN.getEmail();
				  }
				  break;
				  
			  case 7:
				  this.fileCapCCMGBDS = fileCapCCMGBDSMgr.get(id);
				  if(this.fileCapCCMGBDS!=null){
					  fileCode = this.fileCapCCMGBDS.getFileCode();
					  this.sender = this.fileCapCCMGBDS.getContactName();
					  this.email = this.fileCapCCMGBDS.getEmail();
				  }
				  break;
			}
		}
		
		
		return SUCCESS;
	}

	private void updateNotify(String notify_message){
		if (id != null) {
			switch(type)
			{
			  case 1:
				  this.fileCapPhepXayDung = fileCapPhepXDMgr.get(id);
				  if(this.fileCapPhepXayDung!=null){
					  this.fileCapPhepXayDung.setNotify_message(notify_message);
					  fileCapPhepXDMgr.save(fileCapPhepXayDung);
				  }
				  break;			
				  
			  case 2:
				  this.fileCapCCHN = fileCapCCHNMgr.get(id);
				  if(this.fileCapCCHN!=null){
					  this.fileCapCCHN.setNotify_message(notify_message);
					  fileCapCCHNMgr.save(fileCapCCHN);
				  }
				  break;
				  
			  case 3:
				  this.fileCapLaiCCHN = fileCapLaiCCHNMgr.get(id);
				  if(this.fileCapLaiCCHN!=null){
					  this.fileCapLaiCCHN.setNotify_message(notify_message);
					  fileCapLaiCCHNMgr.save(fileCapLaiCCHN);
				  }
				  break;
				  
			  case 4:
				  this.fileDieuChinhGPXD = fileDieuChinhGPXDMgr.get(id);
				  if(this.fileDieuChinhGPXD!=null){
					  this.fileDieuChinhGPXD.setNotify_message(notify_message);
					  fileDieuChinhGPXDMgr.save(fileDieuChinhGPXD);
				  }
				  break;
				  
			  case 5:
				  this.fileGiaHanCPXD = fileGiaHanCPXDMgr.get(id);
				  if(this.fileGiaHanCPXD!=null){
					  this.fileGiaHanCPXD.setNotify_message(notify_message);
					  fileGiaHanCPXDMgr.save(fileGiaHanCPXD);
				  }
				  break;
				  
			  case 6:
				  this.fileBoSungCCHN = fileBoSungCCHNMgr.get(id);
				  if(this.fileBoSungCCHN!=null){
					  this.fileBoSungCCHN.setNotify_message(notify_message);
					  fileBoSungCCHNMgr.save(fileBoSungCCHN);
				  }
				  break;
				  
			  case 7:
				  this.fileCapCCMGBDS = fileCapCCMGBDSMgr.get(id);
				  if(this.fileCapCCMGBDS!=null){
					  this.fileCapCCMGBDS.setNotify_message(notify_message);
					  fileCapCCMGBDSMgr.save(fileCapCCMGBDS);
				  }
				  break;
			}
		}
	}


}
